import numpy as np
import pandas as pd

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

df = pd.read_excel('超市营业额2.xlsx')
df.日期 = pd.to_datetime(df.日期)

print('每7天营业总额'.ljust(20, '='))
print(df.resample('7D', on='日期').sum(numeric_only=True)['交易额'])

print('每7天营业总额'.ljust(20, '='))
print(df.resample('7D', on='日期', label='right').sum(numeric_only=True)['交易额'])

print('每7天营业额平均值'.ljust(20, '='))
func = lambda num: round(num, 2)
print(df.resample('7D', on='日期', label='right').mean(numeric_only=True).apply(func)['交易额'])

print('每7天营业额平均值'.ljust(20, '='))
func = lambda item: round(np.sum(item)/len(item), 2)
print(df.resample('7D', on='日期', label='right')['交易额'].apply(np.mean).round(2))
